package com.arrogames.blondbattle.controller {
	import com.arrogames.blondbattle.game.purchases.events.PurchaseEvent;
	import com.arrogames.blondbattle.game.state.event.GameOverEvent;
	import com.arrogames.blondbattle.GameModel;
	import com.arrogames.blondbattle.game.GameEngine;
	import com.arrogames.blondbattle.model.UnitDefinition;
	import com.arrogames.blondbattle.model.WorldDefinition;
	import com.arrogames.blondbattle.ui.BBNavigator;
	import com.arrogames.blondbattle.ui.buttons.MusicButton;
	import com.arrogames.blondbattle.ui.buttons.WorldButton;
	import com.arrogames.blondbattle.ui.MenuScreen;
	import feathers.controls.ScreenNavigator;
	import org.as3commons.logging.api.getLogger;
	import org.as3commons.logging.api.ILogger;
	import starling.events.Event;
	
	public class MainController {
		
		private static const logger:ILogger = getLogger(MainController);
		
		private var view:BBNavigator;
		private var model:GameModel;
		
		public function MainController(navigator:BBNavigator, model:GameModel) {
			this.view = navigator;
			this.model = model;
			
			//initial setup.
			view.showMenu();
			model.playMenuMusic();
			
			//add events listeners
			view.addEventListener(WorldButton.START, onStartMenu);
			//view.addEventListener(Event.COMPLETE, onStartMenu);
			view.addEventListener(MusicButton.MUTE, onMute);
			view.addEventListener(MusicButton.UNMUTE, onUnMute);
			
			view.addEventListener(PurchaseEvent.PURCHASE, onPurchaseUnit);
			view.addEventListener(GameOverEvent.GAMEOVER, onGameOver);
		}
		
		private function onMute(e:Event):void {
			model.mute();
		}
		
		private function onUnMute(e:Event):void {
			model.unmute();
		}
		
		private function onStartMenu(e:Event):void {
			var world:WorldDefinition = e.data as WorldDefinition;
			
			model.playGameMusic();
			view.showGameField();
			
			model.startGame(world);
		}
		
		private function onPurchaseUnit(e:PurchaseEvent):void {
			model.purchaseUnit(e.unit);
		}
		
		private function onGameOver(e:GameOverEvent):void {
			model.playMenuMusic();
			model.stopGame();
			view.showMenu();
		}
		
	}
}